package com.drill.controller.folder;

import com.drill.controller.article.FrontCacheService;
import com.drill.controller.article.TbArticle;
import com.drill.controller.interceptor.FolderInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jflyfox.jfinal.base.BaseController;
import com.jflyfox.jfinal.base.Paginator;
import com.jflyfox.jfinal.component.annotation.ControllerBind;
import com.jflyfox.util.NumberUtils;
import com.jflyfox.util.StrUtils;

/**
* @auto ket
* @date 2016年6月27日 下午5:08:55
*/
@ControllerBind(controllerKey="/")
public class FolderController extends BaseController{
	
	public static final String PATH = "";
	
	@Before(FolderInterceptor.class)
	public void index(){
		int folderRoot = Folder.ROOT;
		
		String folderStr = getPara();
		
		log.info("folderStr :"+folderStr);
		
		
		
		Integer folderId=folderRoot;
		
		if(folderStr!=null){
			if(NumberUtils.parseInt(folderStr)>0){
				folderId=NumberUtils.parseInt(folderStr);
			}
		}
		
		if (folderId == null || folderId <= 0) {
			folderId = folderRoot;
		}
		
		// 活动目录
		setAttr("folders_selected", folderId);
		
		Folder folder = new FolderService().getFolder(folderId);
		log.info(folder.toString());
		setAttr("folder", folder);
		
		if(folderId>1){
			String pageStr=getAttr("page_no");
			int pageNo=1;
			if(StrUtils.isNotEmpty(pageStr)){
				pageNo=Integer.parseInt(pageStr);
			}
			Page<TbArticle> list=new FrontCacheService().getArticle(new Paginator(pageNo,10), folderId);
			setAttr("list", list);
		}
		
		// 栏目跳转规则
		String jumpUrl = folder.getJumpUrl();
		String path = folder.getPath();
		String urlKey = folder.getKey();
		if (StrUtils.isNotEmpty(jumpUrl)) {
			redirect(jumpUrl);
		} else if (StrUtils.isNotEmpty(path)) {
			render(path);
		} else {
			render(PATH + urlKey + ".html");
		}
	}

}
